package com.laputa.model.manager;

import java.util.*;
import com.laputa.dao.*;
import com.laputa.dao.model.*;
import com.laputa.system.Lib;

public class CityManager{

	private CityDao cityDao;
	private GroundManager groundManager;	
	
	public void setCityDao(CityDao pd){
		this.cityDao = pd;
	}
	
	public void setGroundManager(GroundManager groundManager){
		this.groundManager = groundManager;
	}
	
	public void delete(String id) {
		cityDao.delete(new Long(id));
	}

	public List getAll() {
		return cityDao.getAll();
	}

	public Long getMaxCityID(){
		//TODO: need modify
		List cityList = getAll();
		if (cityList != null){
			return ((City)cityList.get(cityList.size() - 1)).getId();
		}
		else 
			return 1L;
	}
	
	public void createGrounds(Long userId, int groundNum) {
		Ground[] grounds = new Ground[groundNum];
		for (int i = 0; i < groundNum; i++){
			grounds[i] = new Ground();
			grounds[i].setPrice(Lib.emptyGroundPrice);
			grounds[i].setUsetime(Lib.getCurrentTime());
			grounds[i].setOwner(userId);
			grounds[i].setUser(userId);
			grounds[i].setBuilding(-1L);
			groundManager.save(grounds[i]);
		}
	}
	
	public City getCity(String id) {
		return cityDao.getCity(new Long(id));
	}
	
	//TODO: May use Spring Factory to new the city object
	public com.laputa.model.City getCityObject(String id, String userID) {
		com.laputa.model.City city = new com.laputa.model.City();
		City c = getCity(id);
		System.out.println("from citymanager, c = " + c + ", id = " + id);
		city.setgroundCur(c.getGroundCur());
		city.setgroundTot(c.getGroundTot());
		List grounds = groundManager.getGroundObjectsByOwner(userID);
		for (int i = 0; i < grounds.size(); ++i){
			city.setGround((com.laputa.model.Ground)grounds.get(i));
		}
		return city;
	}
	
	public void save(City p) {
		cityDao.save(p);
	}
}
